<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-11-22 9:12:00
 * @modified         2016-11-22 9:12:00
 */

class ControllerAccountOrder1 extends Controller
{
    private $error = array();

    public function cancel()
    {
        $this->load->language('account/order');

        if (isset($this->request->get['order_id'])) {
            $order_id = $this->request->get['order_id'];
        } else {
            $order_id = 0;
        }

        $this->load->model('checkout/order');
        
        $order_info = $this->model_checkout_order->getOrder($order_id);
        
        if($order_info['order_status_id'] == $this->config->get('gdconfig_unpaid_status'))
        $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('gdconfig_cancelled_status'));

        $this->response->redirect($this->url->link('account/order'));
    }

    public function confirm()
    {
        $this->load->language('account/order');

        if (isset($this->request->get['order_id'])) {
            $order_id = $this->request->get['order_id'];
        } else {
            $order_id = 0;
        }

        $this->load->model('checkout/order');
        
        $order_info = $this->model_checkout_order->getOrder($order_id);
        
        if($order_info['order_status_id'] == $this->config->get('gdconfig_shipped_status_id')) {
            $complete_status = $this->config->get('config_complete_status');
            $this->model_checkout_order->addOrderHistory($order_id, $complete_status[0]);
        }

        $this->response->redirect($this->url->link('account/order'));
    }

    public function paymentTimeout()
    {
        $unpaid_status = $this->config->get('config_unpaid_status');
        $hours = $this->config->get('config_unpaid_time');
        $cancelled_status = $this->config->get('config_cancelled_status');

        $sql = "SELECT o.order_id, o.order_status_id FROM `" . DB_PREFIX . "order` o WHERE o.order_status_id = '$unpaid_status' AND o.date_added < date_sub(now(),INTERVAL $hours HOUR )";

        $query = $this->db->query($sql);

        if ($query->rows) {
            $this->load->model('checkout/order');
            foreach ($query->rows as $unpaid_order) {
                $this->model_checkout_order->addOrderHistory($unpaid_order['order_id'], $cancelled_status, '', true);
            }
        }
    }

    public function autoCompleteOrder()
    {
        $shipped_status = $this->config->get('config_shipped_status_id');
        $days = $this->config->get('config_complete_time');
        $complete_status = $this->config->get('config_complete_status');
        $complete_status = $complete_status[0];

        $sql = "SELECT o.order_id, o.order_status_id FROM `" . DB_PREFIX . "order` o WHERE o.order_status_id = '$shipped_status' AND o.date_modified < date_sub(now(),INTERVAL $days DAY )";

        $query = $this->db->query($sql);

        if ($query->rows) {
            $this->load->model('checkout/order');
            foreach ($query->rows as $shipped_order) {
                $this->model_checkout_order->addOrderHistory($shipped_order['order_id'], $complete_status, '', true);
            }
        }
    }
}
